package com.bvgol.examples.nio;/**
 * @Classname TestMappedByteBuffer
 * @Description TODO
 * @Date 2021/6/18 9:44
 * @Created by GUOCHEN
 */

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @program: my-spring-completely
 * @description: 可让文件直接在内存(堆外内存)修改, 操作系统不需要cp一次
 * 非常底层  效率极高
 * @author: GUOCHEN
 * @create: 2021/06/18 09:44
 */
public class TestMappedByteBuffer {

    public static void main(String[] args) throws IOException {
        RandomAccessFile accessFile = new RandomAccessFile("D:\\idea_projects\\my-springboot-completely\\netty\\src\\main\\resources\\file01.txt", "rwrwrwrwrw");

        FileChannel accessFileChannel = accessFile.getChannel();

        /*
            参数1:使用读写模式
            参数2:可以修改的其实位置
            参数3:读进内存的结束位置
            修改范围 0-5 -->  0,1,2,3,4
         */
        MappedByteBuffer mappedByteBuffer = accessFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, accessFileChannel.size());

        mappedByteBuffer.put(0,(byte)'H');
        mappedByteBuffer.put(3,(byte)'H');
        mappedByteBuffer.put(6,(byte)'H');
        mappedByteBuffer.put(9,(byte)'H');



        accessFile.close();
    }
}
